package com.wejoy.video.middle.station.data.service.workorder.impl;

import com.wejoy.video.middle.station.data.domain.system.SysNotice;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderInfo;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderProcess;
import com.wejoy.video.middle.station.data.enums.dict.*;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.mapper.workorder.IWorkOrderProcessMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysNoticeService;
import com.wejoy.video.middle.station.data.service.workorder.IOrderPhotoLibService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderInfoService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderProcessService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 工单处置描述表
 *
 * @author miracle_j
 * @date 2022年11月17日 14:35:58
 */
@Service
public class WorkOrderProcessServiceImpl implements IWorkOrderProcessService {
    @Autowired
    private IWorkOrderProcessMapper workOrderProcessMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private IWorkOrderInfoService workOrderInfoService;
    @Autowired
    private IOrderPhotoLibService orderPhotoLibService;

    @Override
    public void save(WorkOrderProcess req, String operator,Long operatorId) {
        req.setOperator(operator);
        if (null == req.getId()) {
            //如果提交处置信息  需要判断是否到达可处置条件（事件已经end）
//            if(req.getType().equals(EnumProcessType.PROCESS.getCode())&&req.getFalseAlerm()==0){
//                OrderPhotoLib photoLib = new OrderPhotoLib();
//                photoLib.setOrderId(req.getOrderId());
//                photoLib.setCallBackStatus(EnumEvsEventStatus.END.getCode());
//                int count = orderPhotoLibService.getCount(photoLib);
//                if(count==0){
//                    throw new BusinessException("该工单事件尚未结束！暂时不能提交审核！");
//                }
//            }
            WorkOrderInfo workOrderInfo = workOrderInfoService.getOneById(req.getOrderId());
            if(workOrderInfo==null) {
                throw new BusinessException("参数错误！");
            }
            //如果未接单提交误报或者预处理。则视为已接单
            if(workOrderInfo.getAcceptUserId()==null||workOrderInfo.getAcceptTime()==null||workOrderInfo.getAcceptUserName()==null) {
                workOrderInfo.setAcceptTime(new Date());
                workOrderInfo.setAcceptUserId(operatorId);
                workOrderInfo.setAcceptUserName(operator);
                workOrderInfo.setAcceptUserPhone(req.getUserPhone());
                workOrderInfo.setOrderStatus(EnumWorkOrderStatus.ACCEPT_UNPROCESSED.getCode());
            }
            if(!workOrderInfo.getOrderStatus().equals(EnumWorkOrderStatus.ACCEPT_UNPROCESSED.getCode())){
                throw new BusinessException("该工单已被处置过！");
            }
            if(!workOrderInfo.getAcceptUserId().equals(operatorId)){
                throw new BusinessException("您不是接单人");
            }
            insertSelective(req);
            if(req.getType().equals(EnumProcessType.PROCESS.getCode())){
                workOrderInfo.setFalseAlerm(req.getFalseAlerm());
            }
            //如果是已处置提交审核，则需要发起一条通知消息
            if(req.getType().equals(EnumProcessType.PROCESS.getCode())){
                notificationDisposed(workOrderInfo,req, operator, operatorId);
            }else {
                //更新工单最后一次预处置时间
                workOrderInfo.setPreprocessingTime(new Date());
                workOrderInfo.setOrderStatus(EnumWorkOrderStatus.ACCEPT_PREPROCESSING.getCode());
                workOrderInfoService.save(workOrderInfo,  operator);
            }
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(WorkOrderProcess req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return workOrderProcessMapper.insertSelective(req);
    }

    private void notificationDisposed( WorkOrderInfo workOrderInfo,WorkOrderProcess req, String operator,Long operatorId) {
        workOrderInfo.setOrderStatus(EnumWorkOrderStatus.ACCEPT_PROCESSED.getCode());
        workOrderInfo.setDisposalTime(new Date());
        workOrderInfoService.save(workOrderInfo,operator);
        String[] split = workOrderInfo.getManagerUserId().split(",");
        for (int i = 0; i < split.length; i++) {
            SysNotice sysNoticeDB = new SysNotice();
            sysNoticeDB.setOrderId(req.getOrderId());
            sysNoticeDB.setIsRead(EnumReadNoticeType.HAVE_READ.getCode());
            sysNoticeDB.setTitle(EnumNoticeType.AUDIT_NOTICE.getText());
            sysNoticeDB.setType(EnumNoticeType.AUDIT_NOTICE.getCode());
            sysNoticeDB.setRemindType(EnumNoticePopUpsType.WARNING_POPUPS.getCode());
            sysNoticeDB.setFromId(operatorId);
            sysNoticeDB.setToId(Long.valueOf(split[i]));
            sysNoticeDB.setContent(workOrderInfo.getDeptName()+","+workOrderInfo.getAlarmLocation());
            sysNoticeService.save(sysNoticeDB,operator);
        }


    }

    @Override
    public int updateSelectiveById(WorkOrderProcess workOrderProcess) {
        WJAssert.notNull(workOrderProcess, "入参对象不能为空");
        WJAssert.notNull(workOrderProcess.getId(), "id参数错误");
        WorkOrderProcess where = new WorkOrderProcess();
        where.setId(workOrderProcess.getId());
        return workOrderProcessMapper.updateSelective(workOrderProcess, where);
    }

    @Override
    public int getCount(WorkOrderProcess workOrderProcess) {
        checkStringLength(workOrderProcess, true);
        return workOrderProcessMapper.getCount(workOrderProcess);
    }

    @Override
    public WorkOrderProcess getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        WorkOrderProcess where = new WorkOrderProcess();
        where.setId(id);
        return workOrderProcessMapper.getOne(where);
    }

    @Override
    public List<WorkOrderProcess> getList(WorkOrderProcess workOrderProcess) {
        checkStringLength(workOrderProcess, true);
        return workOrderProcessMapper.getList(workOrderProcess);
    }

    @Override
    public List<WorkOrderProcess> getPageList(WorkOrderProcess workOrderProcess) {
        checkStringLength(workOrderProcess, true);
        return workOrderProcessMapper.getPageList(workOrderProcess);
    }

    @Override
    public void audit(Long id, String operator) {
        WorkOrderProcess req = new WorkOrderProcess();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        WorkOrderProcess req = new WorkOrderProcess();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        WorkOrderProcess req = new WorkOrderProcess();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(WorkOrderProcess workOrderProcess, boolean nullAble) {
        WJAssert.notNull(workOrderProcess, "入参对象不能为空");
        WJAssert.limitMaxLength(workOrderProcess.getProcessingDescription(), true, 255, "处理描述不合法!");
        WJAssert.limitMaxLength(workOrderProcess.getOperator(), nullAble, 100, "操作人不合法!");
        WJAssert.limitMaxLength(workOrderProcess.getRemark(), true, 255, "备注不合法!");
    }
}
